﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Minesweeper.Properties;

namespace Minesweeper
{
    public partial class MinesweeperBoard : Form
    {
        private const int SIZE = 32; // the size of the cell
        private int[][] _board; // logic board
        private int _columns = 15; // the minesweeper rows
        private int _rows = 15; // the minesweeper columns

        public MinesweeperBoard()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Get or set the number of columns of the minesweeper board
        /// </summary>
        public int Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }

        /// <summary>
        /// Get or set the number of rows of the minesweeper board
        /// </summary>
        public int Rows
        {
            get { return _rows; }
            set { _rows = value; }
        }

        public int[][] Board
        {
            get { return _board; }
            set { _board = value; }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO:
            // Load settings from configuration XML file
            //
            InitializeLogicBoard();
            InitializeGUIBoard();
        }

        private void InitializeGUIBoard()
        {
            // calculate the size of the board 
            boardPanel.Width = Rows * SIZE;
            boardPanel.Height = Columns * SIZE;

            // Load the minesweeper board
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Columns; j++)
                {
                    var cell = new Cell(i, j, SIZE);
                    boardPanel.Controls.Add(cell);
                    cell.Location = new Point(i * 32, j * 32);
                }
            }
        }

        private void InitializeLogicBoard()
        {
            Board = new int[Rows][];
            for (int i = 0; i < Board.Length; i++)
            {
                Board[i] = new int[Columns];
                for (int j = 0; j < Board[i].Length; j++)
                {
                    Board[i][j] = 0;
                }
            }
        }

        #region Nested type: Cell

        /// <summary>
        /// 
        /// </summary>
        private class Cell : Panel
        {
            private int _column;
            private bool _isClicked; // default Fasle
            private int _row;

            // private int _state;
            // private bool _hasMine;
            // ...

            /// <summary>
            /// 
            /// </summary>
            /// <param name="rowIndex"></param>
            /// <param name="columnIndex"></param>
            /// <param name="size">The size of the cell (Default 32 pixel)</param>
            public Cell(int rowIndex, int columnIndex, int size)
            {
                Size = new Size(size, size);
                base.BackgroundImageLayout = ImageLayout.Zoom;
                base.BackgroundImage = Resources.normalCell;

                Row = rowIndex;
                Column = columnIndex;
            }

            public Cell(int rowIndex, int columnIndex)
                        : this(rowIndex, columnIndex, 32)
            {
            }

            /// <summary>
            /// 
            /// </summary>
            public int Row
            {
                get { return _row; }
                set
                {
                    if (value >= 0)
                    {
                        _row = value;
                    }
                    else
                    {
                        throw new Exception("Out of range");
                    }
                }
            }

            /// <summary>
            /// 
            /// </summary>
            public int Column
            {
                get { return _column; }
                set
                {
                    if (value >= 0)
                    {
                        _column = value;
                    }
                    else
                    {
                        throw new Exception("Out of range");
                    }
                }
            }

            /// <summary>
            /// Handle click event
            /// </summary>
            /// <param name="e"></param>
            protected override void OnClick(EventArgs e)
            {
                base.OnClick(e);

                _isClicked = true;

                // check Left Mouse or Right Mouse to set flag, etc...
                var m = e as MouseEventArgs;

                if (m != null)
                {
                    if (m.Button == MouseButtons.Right)
                    {
                        BackgroundImage = Resources.kmines_32;
                    }
                    if (m.Button == MouseButtons.Left)
                    {
                        BackgroundImage = Resources.num1;
                    }
                }

                MessageBox.Show(string.Format("Cell ({0}; {1}) clicked", Row, Column), "Info",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


            protected override void OnMouseLeave(EventArgs e)
            {
                base.OnMouseLeave(e);
                if (!_isClicked)
                {
                    BackgroundImage = Resources.normalCell;
                }
            }

            protected override void OnMouseEnter(EventArgs e)
            {
                base.OnMouseEnter(e);
                if (!_isClicked)
                {
                    BackgroundImage = Resources.onHover;
                }
            }
        }

        #endregion
    }
}